package rozetka.pages;

import org.apache.xpath.operations.Bool;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import rozetka.utils.ReportWriter;

/**
 * Created by foneox on 06.04.2014.
 */
public class CheckoutPage extends BasePage{
    public static final String EXACT_ACTIVE_MAKEORDER_BUTTON_CLASSNAME = "btn-link check-step-btn-link check-submit";
    private By contactNameLocator = By.id("reciever_name");
    private By contactPhoneLocator = By.id("reciever_phone");
    private By contactCityLocator = By.id("suggest_locality");
    private By nextStepButtonLocator = By.cssSelector
            ("#step_contacts > div > div.check-step-part > div.clearfix.f-i.grid-box-top > div > span > button");
    private By citiesLocator = By.xpath(".//*[@id='locality_id']/..//ul/li");
    private By makeOrderButtonLocator = By.xpath(".//*[@id='make-order']/..");
    private By contactEmail = By.id("reciever_email");

    public CheckoutPage(WebDriver driver) {
        super(driver);
    }

    public void checkout(String customerName, String customerPhone) {
        driver.findElement(contactNameLocator).sendKeys(customerName);
        driver.findElement(contactCityLocator).click(); //opens dropdown
        (new WebDriverWait(driver, 3)).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(citiesLocator));
        driver.findElement(citiesLocator).click(); //selects first city in the list: Kiev
        (new WebDriverWait(driver, 3)).until(ExpectedConditions.visibilityOfElementLocated(contactPhoneLocator));
        driver.findElement(contactPhoneLocator).sendKeys(customerPhone);
        driver.findElement(contactEmail).clear();
        WebElement nextButton = driver.findElement(nextStepButtonLocator);
        nextButton.click();
    }
    public Boolean canOrderBeSubmitted() {
        WebElement makeOrderButton = driver.findElement(makeOrderButtonLocator);
        if (makeOrderButton.getAttribute("class").equals(EXACT_ACTIVE_MAKEORDER_BUTTON_CLASSNAME)){
            return true;
        }else{
            return false;
        }
    }
}
